package com.base.common.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.base.common.dao.CompanyDAO;
import com.base.common.dao.KnowledgeDAO;
import com.base.model.CompanyInfo;
import com.base.model.Knowledge;

public class CompanyServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String method = (String)req.getParameter("method");
		if(method != null){
			if("load".equals(method)){
				this.load(req, resp);
			}else if("update".equals(method)){
				this.update(req, resp);
			}
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		this.doGet(req, resp);
	}

	protected void load(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
		CompanyDAO companyDAO = new CompanyDAO();
		CompanyInfo companyInfo = null;
		companyInfo = companyDAO.load();
		if(companyInfo != null){
			req.setAttribute("companyInfo", companyInfo);
			RequestDispatcher rd = req.getRequestDispatcher("/pages/admin/company/companyManage.jsp");
			rd.forward(req, resp);
		} else{
			resp.sendRedirect("pages/admin/admin.jsp");
			return;
		}
	}
	
	protected void update(HttpServletRequest req, HttpServletResponse resp)
	throws ServletException, IOException {
		String companyId = (String)req.getParameter("companyId");
		String companyName = (String)req.getParameter("companyName");
		String companyAddres = (String)req.getParameter("companyAddres");
		String companyEmail = (String)req.getParameter("companyEmail");
		String companyPostcode = (String)req.getParameter("companyPostcode");
		String companyTel = (String)req.getParameter("companyTel");
		String companyMobile = (String)req.getParameter("companyMobile");
		String companyFax = (String)req.getParameter("companyFax");
		String companyDescription = (String)req.getParameter("companyDescription");
		CompanyDAO dao = new CompanyDAO();
		
		CompanyInfo entity = new  CompanyInfo();
		entity.setCompanyName(companyName);
		entity.setCompanyAddres(companyAddres);
		entity.setCompanyDescription(companyDescription);
		entity.setCompanyEmail(companyEmail);
		entity.setCompanyFax(companyFax);
		entity.setCompanyId(companyId);
		entity.setCompanyMobile(companyMobile);
		entity.setCompanyPostcode(companyPostcode);
		entity.setCompanyTel(companyTel);
		
		boolean success = dao.update(entity);
		if(success){
			req.setAttribute("success", "true");
		}else{
			req.setAttribute("success", "false");
		}
		RequestDispatcher rd = req.getRequestDispatcher("/CompanyServlet.do?method=load");
		rd.forward(req, resp);
	}
}
